140B - New Year Cards - CodeForces Solution


brute force greedy implementation *1800

Please click on ads to support us..

Python Code:

def solve():
    n = int(input())
    f = [list(map(int, input().split())) for _ in range(n)]
    a = list(map(int, input().split()))
    pre = [0 for _ in range(n + 1)]
    res = [0 for _ in range(n)]
    for i in range(n):
        pre[a[i]] = i

    def check(v, r):
        for x in a:
            if x - 1 == r:
                continue
            if x < v:
                return 0
            elif x == v:
                return 1
    for i in range(n):
        for x in f[i]:
            if x - 1 == i:
                continue
            if check(x, i):
                res[i] = x
                break
    for i in res:
        print(i, end=" ")



if __name__ == "__main__":
    _ = 1
        while _:
        solve()
        _ -= 1

 		     			 			 	     		  		 	

C++ Code:

//jiudge: 21793: 1683635347
#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[305][305],f[305],k,n,b[305],t[305];
signed main(){
	cin >> n;
	for (int i=1;i<=n;++i) {
		for (int j=1;j<=n;++j) {
			cin >> a[i][j];
		}
	}
	for (int i=1;i<=n;++i) {
		cin >> b[i];
		t[b[i]]=i;
	}
	for (int i=1;i<=n;++i) {
		t[0]=n+1;k=0;
		for (int j=1;j<=n;++j) f[j]=0;
		for (int j=1;j<=n;++j) {
			if (j==i) continue;
			if (t[j]<t[k]) {
				k=j;
			}
			f[k]=1;
		}
		for (int j=1;j<=n;++j) {
			if (f[a[i][j]]&&a[i][j]!=i) {
				cout << a[i][j] << " ";
				break;
			}
		}
	}
}
//at most one straight through how many circles


Comments

Submit
0 Comments
More Questions

1546B - AquaMoon and Stolen String
1353C - Board Moves
902A - Visiting a Friend
299B - Ksusha the Squirrel
1647D - Madoka and the Best School in Russia
1208A - XORinacci
1539B - Love Song
22B - Bargaining Table
1490B - Balanced Remainders
264A - Escape from Stones
1506A - Strange Table
456A - Laptops
855B - Marvolo Gaunt's Ring
1454A - Special Permutation
1359A - Berland Poker
459A - Pashmak and Garden
1327B - Princesses and Princes
1450F - The Struggling Contestant
1399B - Gifts Fixing
1138A - Sushi for Two
982C - Cut 'em all
931A - Friends Meeting
1594A - Consecutive Sum Riddle
1466A - Bovine Dilemma
454A - Little Pony and Crystal Mine
2A - Winner
1622B - Berland Music
1139B - Chocolates
1371A - Magical Sticks
1253A - Single Push